<template>
  <base-option :selected="selected" :field-config="fieldConfig" :form-config="formConfig">
    <template #default="{ hideOptions }">
      <el-form-item v-if="fieldConfig.defaultValue !== undefined && hideOptions('defaultValue')" label="默认值">
        <el-input v-model="fieldConfig.defaultValue" />
      </el-form-item>
      <el-form-item label="数据源">
        <el-button style="flex: 1" plain @click="openDialog">编辑代码</el-button>
      </el-form-item>
      <el-form-item v-if="fieldConfig.expandTrigger !== undefined && hideOptions('expandTrigger')" label="展开方式">
        <el-radio-group v-model="fieldConfig.expandTrigger">
          <el-radio-button label="click" value="click">click</el-radio-button>
          <el-radio-button label="hover" value="hover">hover</el-radio-button>
        </el-radio-group>
      </el-form-item>
    </template>
  </base-option>
  <el-dialog
    v-model="dialogCode"
    title="数据源"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    destroy-on-close
  >
    <code-editor v-model="optionsJSON" mode="json" />
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="handleSave">保存</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, toRefs } from 'vue'
import baseOption from './base-option.vue'
import propsDefinition from './props'
const props = defineProps(propsDefinition)
const { selected, fieldConfig, formConfig } = toRefs(props)

const dialogCode = ref(false)
const optionsJSON = ref('')

const openDialog = () => {
  optionsJSON.value = JSON.stringify(fieldConfig.value.optionItems || [], null, '  ')
  dialogCode.value = true
}

const handleSave = () => {
  dialogCode.value = false
  fieldConfig.value.optionItems = JSON.parse(optionsJSON.value)
}
</script>
